package backingbean;

import java.io.Serializable;
import java.util.ArrayList;

import javax.annotation.ManagedBean;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;

import dao.CategoryDAORemote;
import dao.DomainDAORemote;
import dao.QuestionDAORemote;
import dto.CategoryDTO;
import dto.DomainDTO;
import dto.QuestionDTO;

@ManagedBean
@SessionScoped
public class DomainBean implements Serializable {

	/**
	 * 
	 */
	@EJB
	DomainDAORemote ddao;
	@EJB
	QuestionDAORemote qdao;
	@EJB
	CategoryDAORemote cdao;
	private static final long serialVersionUID = 1L;
	private ArrayList<String> al;
	private ArrayList<String> categNames;
	private String selectedDomain;
	private String selectedCategory;
	private String questionText;
	private ArrayList<String> checkbox1;
	private ArrayList<String> checkbox2;
	private ArrayList<String> checkbox3;
	private ArrayList<String> checkbox4;
	private UIComponent submitButton;
	private String messageClass;

	private String answer1;
	private String answer2;
	private String answer3;
	private String answer4;

	private String errorMessage;

	public void insert() throws Exception {
		int correct = 0;
		int contor = 0;
		errorMessage = "";
		if (this.checkbox1.size() > 0) {
			contor++;
			correct = correct * 10 + Integer.parseInt(checkbox1.get(0));

		}
		if (this.checkbox2.size() > 0) {
			contor++;
			correct = correct * 10 + Integer.parseInt(checkbox2.get(0));
		}
		if (this.checkbox3.size() > 0) {
			contor++;
			correct = correct * 10 + Integer.parseInt(checkbox3.get(0));
		}
		if (this.checkbox4.size() > 0) {
			contor++;
			correct = correct * 10 + Integer.parseInt(checkbox4.get(0));
		}
		if (selectedCategory != null && !selectedCategory.equals("") && selectedDomain != null
				&& !selectedDomain.equals("")) {
			if (contor > 0 && contor <= 2) {

				if (answer1 != null && !answer1.equals("") && answer2 != null && !answer2.equals("") && answer3 != null
						&& !answer3.equals("") && answer4 != null && !answer4.equals("") && questionText != null
						&& !questionText.equals("")) {
					QuestionDTO qdto = new QuestionDTO();
					qdto.setAnswer1(answer1);
					qdto.setAnswer2(answer2);
					qdto.setAnswer3(answer3);
					qdto.setAnswer4(answer4);
					qdto.setCorrectAnswers(correct);
					qdto.setText(questionText);
					qdto.setCategory(cdao.getCategoryByNameAndDomain(selectedCategory, ddao
							.getIdByDomainName(selectedDomain)));
					qdao.addQuestion(qdto, (cdao.getCategoryByNameAndDomain(selectedCategory, ddao
							.getIdByDomainName(selectedDomain)).getId()));
					answer1 = "";
					answer2 = "";
					answer3 = "";
					answer4 = "";
					questionText = "";
					checkbox1 = null;
					checkbox2 = null;
					checkbox3 = null;
					checkbox4 = null;
					FacesMessage message = new FacesMessage("Question added successfully!");
					FacesContext context = FacesContext.getCurrentInstance();
					context.addMessage(submitButton.getClientId(context), message);
					setMessageClass("success");
				}

				else {
					FacesMessage message = new FacesMessage(
							"The domain, category, possible answers and the question body are mandatory!");
					FacesContext context = FacesContext.getCurrentInstance();
					context.addMessage(submitButton.getClientId(context), message);
					setMessageClass("errorMessage");
				}
			} else {
				FacesMessage message = new FacesMessage("You are required to select one or two correct answers!");
				FacesContext context = FacesContext.getCurrentInstance();
				context.addMessage(submitButton.getClientId(context), message);
				setMessageClass("errorMessage");
			}
		} else {
			FacesMessage message = new FacesMessage("Select a domain and a category first!");
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(submitButton.getClientId(context), message);
			setMessageClass("errorMessage");
		}

	}

	public ArrayList<String> getAl() {
		al = new ArrayList<String>();
		for (DomainDTO ddto : ddao.findAllDomains())
			al.add(ddto.getName());
		setSelectedDomain(al.get(0));
		categNames = new ArrayList<String>();
		for (CategoryDTO cdto : cdao.getCategoryByDomain(ddao.getIdByDomainName(al.get(0))))
			categNames.add(cdto.getName());
		setSelectedCategory(cdao.getCategoryByDomain(ddao.getIdByDomainName(al.get(0))).get(0).getName());

		return al;
	}

	public void findCategory(AjaxBehaviorEvent e) {
		categNames = new ArrayList<String>();
		if (selectedDomain != null && !selectedDomain.equals(""))
			for (CategoryDTO cdto : cdao.getCategoryByDomain(ddao.getIdByDomainName(selectedDomain)))
				categNames.add(cdto.getName());
		else
			errorMessage = "Select a domain first!";
		if (cdao.getCategoryByDomain(ddao.getIdByDomainName(selectedDomain)).size() == 0)
			errorMessage = "No categories for this domain!";

	}

	public void cancel() {
		answer1 = "";
		answer2 = "";
		answer3 = "";
		answer4 = "";
		questionText = "";
		checkbox1 = null;
		checkbox2 = null;
		checkbox3 = null;
		checkbox4 = null;
		selectedCategory = "";
		selectedDomain = "";
		errorMessage = "";

	}

	public String getAnswer1() {
		return answer1;
	}

	public void setAnswer1(String answer1) {
		this.answer1 = answer1;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public String getAnswer2() {
		return answer2;
	}

	public void setAnswer2(String answer2) {
		this.answer2 = answer2;
	}

	public String getAnswer3() {
		return answer3;
	}

	public void setAnswer3(String answer3) {
		this.answer3 = answer3;
	}

	public String getAnswer4() {
		return answer4;
	}

	public void setAnswer4(String answer4) {
		this.answer4 = answer4;
	}

	public ArrayList<String> getCheckbox1() {
		return checkbox1;
	}

	public void setCheckbox1(ArrayList<String> checkbox1) {
		this.checkbox1 = checkbox1;
	}

	public ArrayList<String> getCheckbox2() {
		return checkbox2;
	}

	public void setCheckbox2(ArrayList<String> checkbox2) {
		this.checkbox2 = checkbox2;
	}

	public ArrayList<String> getCheckbox3() {
		return checkbox3;
	}

	public void setCheckbox3(ArrayList<String> checkbox3) {
		this.checkbox3 = checkbox3;
	}

	public ArrayList<String> getCheckbox4() {
		return checkbox4;
	}

	public void setCheckbox4(ArrayList<String> checkbox4) {
		this.checkbox4 = checkbox4;
	}

	public void setAl(ArrayList<String> al) {

		this.al = al;
	}

	public DomainDAORemote getDdao() {
		return ddao;
	}

	public void setDdao(DomainDAORemote ddao) {
		this.ddao = ddao;
	}

	public CategoryDAORemote getCdao() {
		return cdao;
	}

	public void setCdao(CategoryDAORemote cdao) {
		this.cdao = cdao;
	}

	public ArrayList<String> getCategNames() {
		return categNames;
	}

	public void setCategNames(ArrayList<String> categNames) {
		this.categNames = categNames;
	}

	public String getSelectedCategory() {
		return selectedCategory;
	}

	public void setSelectedCategory(String selectedCategory) {
		this.selectedCategory = selectedCategory;
	}

	public void setSelectedDomain(String selectedDomain) {
		this.selectedDomain = selectedDomain;
	}

	public String getSelectedDomain() {
		return selectedDomain;
	}

	public void setQuestionText(String questionText) {
		this.questionText = questionText;
	}

	public String getQuestionText() {
		return questionText;
	}

	/**
	 * @param submitButton
	 *            the submitButton to set
	 */
	public void setSubmitButton(UIComponent submitButton) {
		this.submitButton = submitButton;
	}

	/**
	 * @return the submitButton
	 */
	public UIComponent getSubmitButton() {
		return submitButton;
	}

	/**
	 * @param messageClass
	 *            the messageClass to set
	 */
	public void setMessageClass(String messageClass) {
		this.messageClass = messageClass;
	}

	/**
	 * @return the messageClass
	 */
	public String getMessageClass() {
		return messageClass;
	}
}
